package com.et.machine.manager.api

import androidx.annotation.IntDef
import com.et.machine.manager.api.AbstractUploadParam.Companion.HTTP
import com.et.machine.manager.api.AbstractUploadParam.Companion.TCP
import java.util.concurrent.LinkedBlockingQueue


abstract class AbstractUploadParam(@UploadResultType var type: Int) {

    @IntDef(TCP, HTTP)
    annotation class UploadResultType

    companion object {
        const val TCP = 0
        const val HTTP = 1
    }
}

data class TcpUploadParam(val byteArray: ByteArray) : AbstractUploadParam(TCP) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false
        other as TcpUploadParam
        if (!byteArray.contentEquals(other.byteArray)) return false
        return true
    }

    override fun hashCode(): Int {
        return byteArray.contentHashCode()
    }

    fun parse2TcpBody() {

    }

}

data class HttpUploadParam(var code1: String, var code2: String) : AbstractUploadParam(HTTP) {

    fun parse2HttpParam() {

    }
}


class OutResultUpload {

    fun offerUploadResult(param: AbstractUploadParam) {
        if (param.type == TCP && param is TcpUploadParam) {
            uploadByTcp(param)
        } else if (param.type == HTTP && param is HttpUploadParam) {
            uploadByHttp(param)
        }
    }

    fun uploadTrigger() {
        while (uploadQueue.isNotEmpty()) {
            uploadQueue.poll()?.apply {

            }
        }

        if (uploadFrequencyLimitQueue.isNotEmpty()) {
            uploadFrequencyLimitQueue.poll()?.apply {

            }
        }
    }

    private fun uploadByTcp(param: TcpUploadParam) {
        // TODO upload
    }

    private fun uploadByHttp(param: HttpUploadParam) {
        // TODO upload
    }


    companion object {
        @JvmStatic
        fun getInstance() = Holder.ins
        private val uploadQueue = LinkedBlockingQueue<AbstractUploadParam>()
        private val uploadFrequencyLimitQueue = LinkedBlockingQueue<AbstractUploadParam>()
    }

    object Holder {
        val ins = OutResultUpload()
    }
}